const Koa = require('koa')
const path = require('path')
const app = new Koa()
const bodyparser = require('koa-bodyparser')
const koaBody = require('koa-body')
const error = require('koa-json-error')
const parameter = require('koa-parameter')
const koaStatic = require('koa-static')
const koa2Cors = require('koa2-cors')

const userRouter = require('./router/user')
const homeRouter = require('./router/home')
const wxRouter = require('./router/wx')
app.use(koaStatic(path.join(__dirname, 'public')));
// 错误处理
// app.use(error({
//   postFormat: (e, { stack, ...rest }) => process.env.NODE_ENV === 'production' ? rest : { stack, ...rest }
// }));

app.use(koaBody({
  multipart: true,
  formidable: {
    uploadDir: path.join(__dirname, '/public/uploads'),
    keepExtensions: true,
  },
}));
// 参数校验
app.use(parameter(app));

app.use(koa2Cors())
app.use(bodyparser()) // 处理参数 只能处理json form格式 wxRouter
app.use(userRouter.routes(), userRouter.allowedMethods())
app.use(homeRouter.routes(), homeRouter.allowedMethods())
app.use(wxRouter.routes(), homeRouter.allowedMethods())

app.listen(3000, () => {
  console.log('server port is 3000')
})